{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3af82399-c508-4e87-84b3-b1a3f56234a4",
   "metadata": {},
   "source": [
    "## 2412114333-陈宇轩-实验2-0916"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4819d3b9-563b-419b-98f7-e182ae6d1d92",
   "metadata": {},
   "source": [
    "实验1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "15d505fc-a509-4d55-b827-496ff6697aeb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入十进制整数： 1\n",
      "请输入转换进制（2或8）： 2\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "十进制数：1 = (1) 2进制\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/env python3\n",
    "# coding=utf-8\n",
    "\n",
    "class BaseConverter:\n",
    "   \n",
    "    \n",
    "    def __init__(self, num: int):\n",
    "       \n",
    "        self.num = num\n",
    "\n",
    "    def convert(self, base: int) -> str:\n",
    "       \n",
    "        # 验证目标进制是否合法\n",
    "        if base not in (2, 8):\n",
    "            raise ValueError(\"只支持二进制或八进制转换！\")\n",
    "        \n",
    "        # 处理0的特殊情况\n",
    "        if self.num == 0:\n",
    "            return \"0\"\n",
    "        \n",
    "        # 转换过程\n",
    "        result = []\n",
    "        div = self.num\n",
    "        while div != 0:\n",
    "            div, mod = divmod(div, base)\n",
    "            result.append(str(mod))  # 直接存储字符串类型\n",
    "        \n",
    "        # 反转结果并拼接\n",
    "        result.reverse()\n",
    "        return ' '.join(result)\n",
    "\n",
    "    def show_result(self, base: int):\n",
    "        \n",
    "        converted = self.convert(base)\n",
    "        print(f\"十进制数：{self.num} = ({converted}) {base}进制\")\n",
    "\n",
    "\n",
    "def main():\n",
    "   \n",
    "    # 获取用户输入的十进制数\n",
    "    num = int(input(\"请输入十进制整数：\"))\n",
    "    \n",
    "    # 获取并验证用户输入的目标进制\n",
    "    while True:\n",
    "        try:\n",
    "            base = int(input(\"请输入转换进制（2或8）：\"))\n",
    "            if base not in (2, 8):\n",
    "                raise ValueError\n",
    "            break\n",
    "        except ValueError:\n",
    "            print(\"输入错误！请输入2或8。\")\n",
    "    \n",
    "    # 执行转换并显示结果\n",
    "    converter = BaseConverter(num)\n",
    "    converter.show_result(base)\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fb8521bb-d2ef-4e59-ab96-b1c98f4bc413",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-09-16 09:03:35.482164 哥们，要微哨打卡啦...\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[4], line 35\u001b[0m\n\u001b[0;32m     31\u001b[0m     reminder\u001b[38;5;241m.\u001b[39mstart()\n\u001b[0;32m     34\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m---> 35\u001b[0m     main()\n",
      "Cell \u001b[1;32mIn[4], line 31\u001b[0m, in \u001b[0;36mmain\u001b[1;34m()\u001b[0m\n\u001b[0;32m     29\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mmain\u001b[39m():\n\u001b[0;32m     30\u001b[0m     reminder \u001b[38;5;241m=\u001b[39m ClockReminder(\u001b[38;5;241m9\u001b[39m, \u001b[38;5;241m3\u001b[39m)\n\u001b[1;32m---> 31\u001b[0m     reminder\u001b[38;5;241m.\u001b[39mstart()\n",
      "Cell \u001b[1;32mIn[4], line 24\u001b[0m, in \u001b[0;36mClockReminder.start\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m     22\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mstart\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[0;32m     23\u001b[0m     \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m---> 24\u001b[0m         \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mwait_until_time()\n\u001b[0;32m     25\u001b[0m         \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mwarning()\n\u001b[0;32m     26\u001b[0m         time\u001b[38;5;241m.\u001b[39msleep(\u001b[38;5;241m60\u001b[39m)\n",
      "Cell \u001b[1;32mIn[4], line 20\u001b[0m, in \u001b[0;36mClockReminder.wait_until_time\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m     18\u001b[0m     \u001b[38;5;28;01mbreak\u001b[39;00m\n\u001b[0;32m     19\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m---> 20\u001b[0m     time\u001b[38;5;241m.\u001b[39msleep(\u001b[38;5;241m60\u001b[39m)\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "# coding=utf8\n",
    "import datetime\n",
    "import time\n",
    "\n",
    "\n",
    "class ClockReminder:\n",
    "    def __init__(self, hour: int, minute: int):\n",
    "        self.hour = hour\n",
    "        self.minute = minute\n",
    "\n",
    "    def warning(self):\n",
    "        print(f\"{datetime.datetime.now()} 哥们，要微哨打卡啦...\")\n",
    "\n",
    "    def wait_until_time(self):\n",
    "        while True:\n",
    "            now = datetime.datetime.now()\n",
    "            if now.hour == self.hour and now.minute == self.minute:\n",
    "                break\n",
    "            else:\n",
    "                time.sleep(60)\n",
    "\n",
    "    def start(self):\n",
    "        while True:\n",
    "            self.wait_until_time()\n",
    "            self.warning()\n",
    "            time.sleep(60)\n",
    "\n",
    "\n",
    "def main():\n",
    "    reminder = ClockReminder(9, 3)\n",
    "    reminder.start()\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "933514dc-d203-48e3-9bd8-7c0906c5e5e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6 是完全数，真因子为 [1, 2, 3]\n",
      "28 是完全数，真因子为 [1, 2, 4, 7, 14]\n",
      "496 是完全数，真因子为 [1, 2, 4, 8, 16, 31, 62, 124, 248]\n",
      "计算完成，共用时 0.033 秒\n"
     ]
    }
   ],
   "source": [
    "# coding=utf8\n",
    "import time\n",
    "\n",
    "\n",
    "def get_factors(n: int):\n",
    "    return [j for j in range(1, n // 2 + 1) if n % j == 0]\n",
    "\n",
    "\n",
    "def find_perfect_numbers(limit: int):\n",
    "    perfect_numbers = []\n",
    "    for i in range(2, limit + 1):\n",
    "        factors = get_factors(i)\n",
    "        if i == sum(factors):\n",
    "            perfect_numbers.append((i, factors))\n",
    "    return perfect_numbers\n",
    "\n",
    "\n",
    "def main():\n",
    "    start = time.time()\n",
    "    results = find_perfect_numbers(1000)\n",
    "    if results:\n",
    "        for num, factors in results:\n",
    "            print(f\"{num} 是完全数，真因子为 {factors}\")\n",
    "    else:\n",
    "        print(\"没有找到完全数。\")\n",
    "    end = time.time()\n",
    "    print(\"计算完成，共用时 %.3f 秒\" % (end - start))\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "98d50037-76c0-4807-b1f8-edf648013a64",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入评委人数： 6\n",
      "请输入第1个评委的分数： 20\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该评委的分数错误！重新输入：\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入第1个评委的分数： 30\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该评委的分数错误！重新输入：\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入第1个评委的分数： 20\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该评委的分数错误！重新输入：\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入第1个评委的分数： 50\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该评委的分数错误！重新输入：\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入第1个评委的分数： 506\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该评委的分数错误！重新输入：\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入第1个评委的分数： \n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该评委的分数错误！重新输入：\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入第1个评委的分数： 50\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该评委的分数错误！重新输入：\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入第1个评委的分数： 60\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该评委的分数错误！重新输入：\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入第1个评委的分数： 30\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该评委的分数错误！重新输入：\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入第1个评委的分数： 20\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该评委的分数错误！重新输入：\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入第1个评委的分数： 10\n",
      "请输入第2个评委的分数： 1\n",
      "请输入第3个评委的分数： 2\n",
      "请输入第4个评委的分数： 3\n",
      "请输入第5个评委的分数： 5\n",
      "请输入第6个评委的分数： 5\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------评委打的原始分--------------------------- \n",
      " [10.0, 1.0, 2.0, 3.0, 5.0, 5.0]\n",
      "----------------------去掉最高分和最低分后的分数---------------------- \n",
      " [2.0, 3.0, 5.0, 5.0]\n",
      "-------------------------------最后得分------------------------------ \n",
      " 3.75\n"
     ]
    }
   ],
   "source": [
    "# coding=utf8\n",
    "from copy import deepcopy\n",
    "\n",
    "class ScoreCalculator:\n",
    "    def __init__(self):\n",
    "        self.num = 0\n",
    "        self.scores = []\n",
    "        self.original_scores = []\n",
    "        \n",
    "    def input_judge_count(self):\n",
    "        while True:\n",
    "            try:\n",
    "                self.num = int(input(\"请输入评委人数：\"))\n",
    "                if self.num <= 2:\n",
    "                    print(\"评委人数太少，评委人数必须多于 2 人！\")\n",
    "                else:\n",
    "                    break\n",
    "            except:\n",
    "                pass\n",
    "                \n",
    "    def input_scores(self):\n",
    "        for x in range(self.num):\n",
    "            while True:\n",
    "                try:\n",
    "                    score = float(input(f\"请输入第{x+1}个评委的分数：\"))\n",
    "                    assert 0 <= score <= 10\n",
    "                    self.scores.append(score)\n",
    "                    break\n",
    "                except:\n",
    "                    print(\"该评委的分数错误！重新输入：\")\n",
    "        self.original_scores = deepcopy(self.scores)\n",
    "        \n",
    "    def calculate_result(self):\n",
    "        high = max(self.scores)\n",
    "        low = min(self.scores)\n",
    "        self.scores.remove(high)\n",
    "        self.scores.remove(low)\n",
    "        return round(sum(self.scores) / len(self.scores), 3)\n",
    "        \n",
    "    def display_results(self, mean_score):\n",
    "        print(\"评委打的原始分\".center(60, '-'), '\\n', self.original_scores)\n",
    "        print(\"去掉最高分和最低分后的分数\".center(57, '-'), '\\n', self.scores)\n",
    "        print(\"最后得分\".center(65, '-'), '\\n', mean_score)\n",
    "        \n",
    "    def run(self):\n",
    "        self.input_judge_count()\n",
    "        self.input_scores()\n",
    "        mean_score = self.calculate_result()\n",
    "        self.display_results(mean_score)\n",
    "\n",
    "def main():\n",
    "    calculator = ScoreCalculator()\n",
    "    calculator.run()\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34bddbf6-df86-4d3b-a2e0-5a830717f804",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
